package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;


/**
 * Created by Administrator on 2016/12/27.
 */

public class PicWallTransferAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<YNPhoto> datas;

    public PicWallTransferAdapter(Context context, List<YNPhoto> datas) {
        this.context = context;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_pic_select;
        RelativeLayout ll_item_album_wall;                                    //列表View
        SimpleDraweeView iv_item_album_wall_avatar;                            //照片墙图片

        public ViewHolder(final View itemView) {
            super(itemView);
            ll_item_album_wall = (RelativeLayout) itemView
                    .findViewById(R.id.ll_item_album_wall);
            iv_item_album_wall_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_item_album_wall_avatar);
            iv_pic_select = ((ImageView) itemView.findViewById(R.id.iv_pic_select));
            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    ViewGroup.LayoutParams vlp = itemView.getLayoutParams();
                    vlp.height = itemView.getMeasuredWidth();
                    vlp.width = itemView.getMeasuredWidth();
                    itemView.setLayoutParams(vlp);
                    return true;
                }
            });
        }

        public void bindData(final YNPhoto listItems, final int position) {
            if (listItems != null) {
                Uri uri = Uri.parse(listItems.getThumbnail());
                Log.d("yuhao", "listItems.getThumbnail()="+listItems.getThumbnail());
                ImageOptHelper.showFrescoImg(uri, iv_item_album_wall_avatar, context);

                // 点击列表
                ll_item_album_wall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_pic_select.setImageResource(R.drawable.pic_select_import);

                    }
                });
            }
        }
    }

    private YNPhoto getItem(int position) {
        return datas.get(position);
    }
    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_album_wall_transfer, parent, false);
        return new PicWallTransferAdapter.ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof AlbumWallAdapter.ViewHolder) {
            final AlbumWallAdapter.ViewHolder holder = (AlbumWallAdapter.ViewHolder) rv_holder;
            YNPhoto listItems = getItem(position);
            holder.bindData(listItems, position);
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public int getIdByDirection(boolean isTop) {
        if (isTop) {
            if (datas.size() <= 0) {
                return 0;
            }
            return datas.get(0).getPid();
        } else {
            return datas.get(datas.size() - 1).getPid();
        }
    }

    public void addItemsFromBottom(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(datas.size(), items);
//        notifyItemRangeInserted(datas.size(), items.size());
        notifyDataSetChanged();
    }

    public void addItemsFromTop(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(0, items);
//        notifyItemRangeInserted(0, items.size());
        notifyDataSetChanged();
    }
}
